/*
Replication code for "City Limits to Partisan Polarization in the American Public"
by A. Jensen, W. Marble, K. Scheve, and M. Slaughter. 

This master file runs all files needed to generate the results in the paper and
appendix EXCEPT for the hierarchical model results -- which have a separate master
file. 

This file runs the analysis for Figures 1 through 3 in the main text, Figures A-1 
through A-4 in the appendix, and Tables A-1 through A-6 in the appendix. 

NOTE: there is one manual step in this master file -- editing a text file to fix
unicode errors. The replication data has the cleaned version of the file, so this 
do-file will run regardless of whether you do this step. But a complete replication 
would do this manual step as well. Instructions are in the file generate_text_data.do.

This file also calls two R files using the -shell- command. This requires entering
the path to the R executable as a macro. If you're running this file on a Mac
the code will probably work unaltered. On a PC you will need to change
the path to the R executable. See this Statalist thread for help:
https://www.statalist.org/forums/forum/general-stata-discussion/general/1415988-running-r-script-through-terminal-via-shell-from-stata

The last file run here does a tiny bit of cleaning that's necessary for the
hierarchical model results. 
*/

* Local directory 
cd ~/Dropbox/Cities/Publication_Files // edit this line
gl localdir `c(pwd)'                  // DON'T edit this line


* Path to R executable
gl rexec "/usr/local/bin/R" 



* Log running of the code
cap log close
log using "$localdir/Output/Results1.smcl", replace


*** Code to generate the analysis data and run summary statistics (Table A-1) ****
do "$localdir/Code/generate_analysis_data.do"



*** Code to run the conjoint analyses (Figure 2, 3, A-4, and Table A-5, A-6)
do "$localdir/Code/conjoint_analysis.do"



*** Code to run policy importance bar graphs (Appendix Figure A-1, A-2, A-3)
do "$localdir/Code/policy_importance_analysis.do"




******* Code to run word statistics (Table A-2, A-3, A-4) *******
* First file generates the text data. 
do "$localdir/Code/generate_text_data.do"

// NOTE: Now, implement a few manual edits to txt file from "generate_text_data.do", 
// described in the do-file, then run R code "word_count.R" in the next line.
// The replication archive has this step already complete (in the file q63_clean.txt). 

* Run R file to count words (can also do outside of stata)
cd $localdir
shell $rexec --vanilla <"$localdir/Code/word_count.R"


* run final text analysis script, which makes Table A-2, A-3, and A-4 
do "$localdir/Code/text_analysis.do"


******* Code to create Figure 1 *******
* Runs regressions underlying fig. 1, showing polarization over nat'l issues in our sample
do "$localdir/Code/polarization_natissues.do"

* creates plot seen in fig 1. R code, can also be run outside Stata
cd $localdir
shell $rexec --vanilla <"$localdir/Code/polarization_natissues_coefplot.R"


* remove stray file created by the R script
rm "$localdir/Rplots.pdf"


******* Cleaning code for hierarchical model *******
do "$localdir/Code/hlm/01prepare_data.do"

log close
